package ca.psquared.superbrixgame;

import com.google.common.collect.Maps;
import org.newdawn.slick.Color;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.SlickException;

import java.util.Map;

public final class ImageFactory {

    // Non-instantiable
    private ImageFactory() {
    }

    private static Map<Color, Image> imageMap = Maps.newHashMap();

    public static Image getImage(Color color) {

        final int size = GameSettings.BLOCK_SIZE;

        final Image image;

        if (imageMap.containsKey(color)) {
            image = imageMap.get(color);
        } else {

            try {

                image = new Image(size, size);
                final Graphics g = image.getGraphics();
                g.setColor(color.darker(0.5f));
                g.fillRect(0, 0, size, size);
                g.setColor(color);
                g.drawRect(0, 0, size - 1, size - 1);
                g.flush();

                imageMap.put(color, image);

            } catch (SlickException ex) {
                throw new RuntimeException(ex);
            }
        }

        return image.copy();
    }
}
